package test1;


import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            // 连接到服务器，服务器地址为 localhost，端口为 12345
            Socket socket = new Socket("localhost", 12345);
            System.out.println("连接到服务器...");

            // 获取输入流和输出流
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 从控制台输入要发送给服务器的消息
            System.out.print("请输入要发送给服务器的消息: ");
            String message = userInput.readLine();

            // 发送消息到服务器
            output.println(message);

            // 接收来自服务器的响应
            String serverResponse = input.readLine();
            System.out.println("收到服务器响应: " + serverResponse);

            // 关闭连接
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

